package com.luo.shiro;


import com.luo.util.MD5;
import com.luo.util.SpringUtil;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class CustomerRealm extends AuthorizingRealm {

    public static final String REAM_NAME = "OwnRealm";

    //权限认证
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        System.out.printf("AuthorizationInfo===========拦截到访问地址 ，进入权限验证");

        SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
        //授权进入  /vue这个路径  注释掉 ，就没有权限进入，而跳转到未授权页面
//        authorizationInfo.addStringPermission("/vue");
/**
 *
 * 示例代码  了解下怎么实现就好
 *
 *  UserInfo userInfo=(UserInfo)principalCollection.getPrimaryPrincipal();
 SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
 //查询权限
 UserRoleExample userRoleExample=new UserRoleExample();
 UserRoleExample.Criteria criteria=userRoleExample.createCriteria();
 criteria.andUserIdEqualTo(userInfo.getUserId());
 List<UserRole> userRoles=userRoleMapper.selectByExample(userRoleExample);
 if(userRoles!=null && userRoles.size()>0){
 List<Long> roleIds=new ArrayList<>();
 for(UserRole ur:userRoles){
 roleIds.add(ur.getRoleId());
 }

 RoleExample roleExample=new RoleExample();
 RoleExample.Criteria roleCriteria=roleExample.createCriteria();
 roleCriteria.andIdIn(roleIds);
 roleCriteria.andStatusEqualTo(1);
 List<Role> roles=roleMapper.selectByExample(roleExample);
 if(roles!=null && roles.size()>0){
 for(Role role:roles){
 authorizationInfo.addRole(role.getRoleName());

 //查询角色权限
 RolePowerExample rolePowerExample=new RolePowerExample();
 RolePowerExample.Criteria rolePowerCriteria=rolePowerExample.createCriteria();
 rolePowerCriteria.andRoleIdEqualTo(role.getId());
 List<RolePower> rolePowers=rolePowerMapper.selectByExample(rolePowerExample);
 if(rolePowers!=null && rolePowers.size()>0){
 List<Long> powerIds=new ArrayList<>();
 for(RolePower rolePower:rolePowers){
 powerIds.add(rolePower.getPowerId());
 }
 PowerExample powerExample=new PowerExample();
 PowerExample.Criteria powerCriteria=powerExample.createCriteria();
 powerCriteria.andIdIn(powerIds);
 powerCriteria.andStatusEqualTo(1);
 List<Power> powerList=powerMapper.selectByExample(powerExample);
 if(powerList!=null && powerList.size()>0){
 for(Power power:powerList){
 authorizationInfo.addStringPermission(power.getPower());
 }
 }
 }
 }
 }
 }


 //        authorizationInfo.addRole("file");

 return authorizationInfo;
 * */


//        return authorizationInfo;
        return authorizationInfo;
    }


    //登陆认证
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        System.out.printf("AuthenticationInfo===========进入shiro登陆验证");
        try {
            UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken;
            String userName = token.getUsername();
            String password= new String(token.getPassword());
            password= MD5.GetMD5Code(password);
          /*  password= MD5.GetMD5Code(password);*/
//            UserInfoExample userInfoExample=new UserInfoExample();
//            UserInfoExample.Criteria userInfoCriteria=userInfoExample.createCriteria();
//            userInfoCriteria.andUserNameEqualTo(userName);
//            userInfoCriteria.andPasswordEqualTo(password);
//
//            UserInfoMapper userInfoMapper= SpringUtil.getBean(UserInfoMapper.class);
//            //有后台登录权限才允许登录
//            List<UserInfo> userInfos=userInfoMapper.selectByExample(userInfoExample);
//            if(userInfos==null || userInfos.size()==0){
//                throw new Exception("账号或密码错误");
//            }
//            RoleMapper roleMapper=SpringUtil.getBean(RoleMapper.class);
//            RoleExample roleExample=new RoleExample();
//            RoleExample.Criteria roleCriteria=roleExample.createCriteria();
//            roleCriteria.andRoleNameEqualTo("AdminLogin");
//            roleCriteria.andStatusEqualTo(1);
//            roleCriteria.andTypeEqualTo(2);
//            List<Role> roles=roleMapper.selectByExample(roleExample);
//            if(roles==null || roles.size()==0){
//                throw new Exception("请设置后台登录角色");
//            }
//            UserInfo userInfo= userInfos.get(0);
//            UserRoleExample userRoleExample=new UserRoleExample();
//            UserRoleExample.Criteria userRoleCriteria=userRoleExample.createCriteria();
//            userRoleCriteria.andUserIdEqualTo(userInfo.getUserId());
//            userRoleCriteria.andRoleIdEqualTo(roles.get(0).getId());
//            List<UserRole> userRoles=userRoleMapper.selectByExample(userRoleExample);
//            if(userRoles==null || userRoles.size()==0){
//                throw new Exception("无后台登录权限");
//            }
//
//            userInfo.setUserName(userInfo.getUserName());
//            userInfo.setUserId(userInfo.getUserId());
            char[] passwd = token.getPassword();
//            AuthenticationInfo info = new SimpleAuthenticationInfo(userInfo, passwd, REAM_NAME);
            AuthenticationInfo info = new SimpleAuthenticationInfo(passwd, passwd, REAM_NAME);




//            //添加菜单
//            List<Menu> menus= menuMapper.getHasPowerMenuDir(userInfo.getUserId());
//            userInfo.setMenus(menus);
//            if(menus!=null && menus.size()>0){
//                for(Menu menu:menus){
//                    Map param=new HashMap<>();
//                    param.put("userId",userInfo.getUserId());
//                    param.put("pId",menu.getMenuId());
//                    List<Menu> childMenu=menuMapper.getPowerByParentId(param);
//                    menu.setChildren(childMenu);
//                }
//            }



            return info;
        } catch (Exception e) {
            e.printStackTrace();
            throw  new AuthenticationException(e.getMessage());
        }
    }

}
